<?php

/**
 * 订单相关
 */

namespace app\admin\controller;

use think\Db;
use think\Exception;

class Order extends Base


{
    /**
     * 添加商品到订单
     */
    public function stage_goods_add()
    {
        $data = input('');
        // dump($data);
        if (!request()->has('stage_id')) {
            $this->error('提示:没有工程');
        }
        if (!request()->has('goods')) {
            $this->error('提示:没有商品');
        }

        if (!request()->has('songhuo')) {
            $this->error('提示:没有送货时间');
        }


        Db::startTrans();
        try {

            $stage_id = $data['stage_id'];
            $songhuo = strtotime($data['songhuo']);
            $goods = $data['goods'];
            $stage_order_table = Db::name('stage_order');
            $stage_goods_table = Db::name('stage_goods');
            foreach ($goods as $index => $good) {

                $order_data = [];
                $order_data['order_id'] = cmf_get_order_sn();
                $order_data['create_time'] = time();
                $order_data['shop_name'] = $good['name'];
                $order_data['shop_uid'] = $good['shop_uid'];
                $order_data['stage_id'] = $stage_id;
                $order_data['songhuo'] = $songhuo;
                $order_id = $stage_order_table->insertGetId($order_data);
                $goods_data = $good['goods'];
                foreach ($goods_data as $index => $goods_datum) {
                    $li = [];
                    $li['order_id'] = $order_data['order_id'];
                    $li['goods_id'] = $goods_datum['id'];
                    $li['goods_name'] = $goods_datum['name'];
                    $li['goods_num'] = $goods_datum['num'];
                    $li['goods_price'] = $goods_datum['price'];
                    $li['goods_notice'] = $goods_datum['notice'];
                    $li['create_time'] = time();
                    $stage_goods_table->insertGetId($li);
                }


            }
            Db::commit();
            $this->success('操作成功');

        } catch (Exception $exception) {
            Db::rollback();
            $this->error('操作失败');
        }


    }

    /**
     * 获取阶段商品
     */
    public function get_stage_goods()
    {

        $data = input('');
        // dump($data);
        if (!request()->has('stage_id')) {
            $this->error('提示:没有工程');
        }

        $order_status=[];
        $order_status[0]='待处理';
        $order_status[1]='处理中';
        $order_status[2]='已送达';

        $stage_id = $data['stage_id'];
        $stage_order_table = Db::name('stage_order');
        $stage_goods_table = Db::name('stage_goods');
        $orders = $stage_order_table->where('stage_id', $stage_id)->select();
        foreach ($orders as $index => $order) {
            $orders[$index]['songhuo'] = date('n月j日', $order['songhuo']);
            $orders[$index]['create_time'] = date('n月j日', $order['create_time']);
            $orders[$index]['status_text'] = $order_status[$order['order_status']];
            $orders[$index]['goods'] = $stage_goods_table->where('order_id', $order['order_id'])->select();

        }

        return json(['code' => 1, 'data' => $orders]);


    }


}